import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import HotelDetail from '../views/HotelDetail.vue'
import Login from '../views/Login.vue'
import backendRouter from './backend'
import Register from '../views/login/Register.vue'
import Reservation from '../views/Reservation.vue'
import OrderDetail from '../views/OrderDetail.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/hotel/:id',
    name: 'hotel-detail',
    component: HotelDetail,
    props: true
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  },
  {
    path: '/reservation',
    name: 'reservation',  // 修正：去掉前面的斜杠
    component: Reservation,
    props: (route) => ({
      query: route.query
    })
  },
  {
  path: '/orders/:orderId',
  name: 'order-detail', // 命名路由
  component: () => import('@/views/OrderDetail.vue'),
  props: true // 可选：将参数自动转为props
},
{
  path: '/orders',
  name: 'UserOrders',
  component: () => import('@/views/user/UserOrders.vue'),
  meta: { requiresAuth: true }
},
{
  path: '/profile',
  name: 'UserProfile',
  component: () => import('@/views/user/UserProfile.vue'),
  meta: { requiresAuth: true }
},
  ...backendRouter
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  // 可选：添加路由滚动行为
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

export default router